package com.thrd.taiexhibition.fragement;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.thrd.basicframework.bean.BaseFragment;
import com.thrd.basicframework.bean.BaseRecyclerAdapter;
import com.thrd.basicframework.ui.defaultview.LoadingState;
import com.thrd.basicframework.ui.recyclerview.RecyclerViewIndicator;
import com.thrd.basicframework.ui.recyclerview.SpacesItemDecoration;
import com.thrd.taiexhibition.BaseApplication;
import com.thrd.taiexhibition.R;
import com.thrd.taiexhibition.activity.HomeActivity;
import com.thrd.taiexhibition.adapter.ThemeExhibitionBottomAdapter;
import com.thrd.taiexhibition.adapter.ThemeExhibitionTopAdapter;
import com.thrd.taiexhibition.bean.BeanThemeHome;
import com.thrd.taiexhibition.bean.BeanThemeIe;
import com.thrd.taiexhibition.bean.BeanThemeNearby;
import com.thrd.taiexhibition.bean.CityEvent;
import com.thrd.taiexhibition.presener.ThemeExhibitonPresener;
import com.thrd.taiexhibition.ui.TimeADView;
import com.thrd.taiexhibition.view.ThemeExhibitonView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * @author: ${唐杰}
 * @date: 2021/3/22
 */
public class ThemeExhibitionFragment extends BaseFragment implements ThemeExhibitonView {

    private RecyclerViewIndicator mRecyclerViewIndicator;
    private RecyclerView mRecyclerView;
    private ThemeExhibitonPresener mThemeExhibitonPresener;
    private ThemeExhibitionTopAdapter mAdapterTop;
    private ThemeExhibitionBottomAdapter mAdapterBottom;
    private TimeADView mTimeADView;
    private TextView mMore;
    private long cityId;
    private long tempCityID;
    @Override
    protected int setLayoutResourceID() {
        return R.layout.fragment_theme_exhibition;
    }

    @Override
    protected void setUpView(View mContentView) {
        mRecyclerViewIndicator = mContentView.findViewById(R.id.rvi_recycler);
        LinearLayoutManager managerTop=  new LinearLayoutManager(getMContext());
        managerTop.setOrientation(RecyclerView.HORIZONTAL);
        mRecyclerViewIndicator.setLayoutManager(managerTop);
        mRecyclerView = mContentView.findViewById(R.id.mRecyclerView);
        mTimeADView = mContentView.findViewById(R.id.tadv_ad);

        mMore = mContentView.findViewById(R.id.tv_more);
        LinearLayoutManager manager=  new LinearLayoutManager(getMContext());
        manager.setOrientation(RecyclerView.HORIZONTAL);
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.addItemDecoration(new SpacesItemDecoration(0,0,20,0));
    }

    @Override
    protected void setUpData() {
        mThemeExhibitonPresener = new ThemeExhibitonPresener(this);

        mAdapterTop = new ThemeExhibitionTopAdapter(getMContext(),null);
        mRecyclerViewIndicator.setAdapter(mAdapterTop);

        mAdapterBottom = new ThemeExhibitionBottomAdapter(getMContext(),null);
        mRecyclerView.setAdapter(mAdapterBottom);

        /**
         * 广告倒计时结束回调
         */
        mTimeADView.setOnBackClick(new TimeADView.OnBackClick() {
            @Override
            public void OnBackClick() {
                HomeActivity mHomeActivity = (HomeActivity) getActivity();
                if(mHomeActivity!=null){
                    mHomeActivity.recoveryAdAnimention();
                }
            }
        });
        /**
         * 没有数据的情况点击重试
         */
//        mDefaultView.setOnRetryClick(new DefaultView.OnRetryClick() {
//            @Override
//            public void onRetryClick() {
//                mThemeExhibitonPresener.getThemeExhibitonData(cityId);
//            }
//        });

        /**
         * 点击更多
         */
        mMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                if(mBeanThemeHome!=null&&mBeanThemeHome.){
//                    DialogUtils.showQRDialog(getContext(),)
//                }
            }
        });

        /**
         * 点击切换城市
         */
        mAdapterBottom.setItemClick(new BaseRecyclerAdapter.ItemClick<BeanThemeNearby>() {
            @Override
            public void onItemClick(int p, BeanThemeNearby mBeanCity) {
                HomeActivity activity= (HomeActivity) getActivity();
                if(mBeanCity!=null&&activity!=null){
                    activity.setCitySetting(mBeanCity.getCityname(),mBeanCity.getId());
                }
            }
        });
    }

    @Override
    public void onFragmentFirstVisible() {
        super.onFragmentFirstVisible();
        BaseApplication mBaseApplication = (BaseApplication) getActivity().getApplication();
        if(mThemeExhibitonPresener!=null&&mBaseApplication!=null){
//            mThemeExhibitonPresener.getThemeExhibitonData(mBaseApplication.getCityId());
            mThemeExhibitonPresener.getThemeExhibitonData(mBaseApplication.getCityId());
        }



    }

    @Override
    public void onFragmentPause() {
        super.onFragmentPause();
        if(mRecyclerViewIndicator!=null){
            mRecyclerViewIndicator.onFragmentPause();
        }
        HomeActivity mHomeActivity = (HomeActivity) getActivity();
        if(mTimeADView!=null&&mHomeActivity!=null&&mTimeADView.isShow()){
            mHomeActivity.recoveryAdAnimention();
        }
    }

    @Override
    public void onFragmentResume() {
        super.onFragmentResume();
        if(mRecyclerViewIndicator!=null){
            mRecyclerViewIndicator.onFragmentResume();
        }
        HomeActivity mHomeActivity = (HomeActivity) getActivity();
        if(mTimeADView!=null&&mHomeActivity!=null&&mTimeADView.isShow()){
            mHomeActivity.startAdAnimention();
        }

        if(mThemeExhibitonPresener!=null&&tempCityID>0){
            mThemeExhibitonPresener.getThemeExhibitonData(tempCityID);
            tempCityID=-1;
        }


    }

    @Override
    public void onError(String error, int type) {
        getContentView().setState(LoadingState.STATE_ERROR);
    }

    private BeanThemeHome mBeanThemeHome;
    @Override
    public void onSuccess(BeanThemeHome mBeanThemeHome) {
        this.mBeanThemeHome=mBeanThemeHome;
        if(mBeanThemeHome!=null){
            if(mBeanThemeHome.getSystemThemeExhibitionAdvertisementBO()!=null){
                mTimeADView.setData(mBeanThemeHome.getSystemThemeExhibitionAdvertisementBO().getImgSrc(),5);
                HomeActivity mHomeActivity = (HomeActivity) getActivity();
                if(mHomeActivity!=null&&isCurrentVisibleState()){
                    mHomeActivity.startAdAnimention();
                }
            }
            if(mBeanThemeHome.getIe()!=null){
                mAdapterTop.refresh(mBeanThemeHome.getIe());
                mRecyclerViewIndicator.forceUpdateItemCount();
                mAdapterTop.setItemClick(new BaseRecyclerAdapter.ItemClick<BeanThemeIe>() {
                    @Override
                    public void onItemClick(int p, BeanThemeIe mBeanThemeIe) {
                      //  ARouter.getInstance().build("/app/tda").withSerializable("BeanThemeIe",mBeanThemeIe) .navigation();
                        ARouter.getInstance().build("/app/tiha") .withLong("id",mBeanThemeIe.getId()).navigation();
                    }
                });

            }

            if(mBeanThemeHome.getNearbyCity()!=null){
                mAdapterBottom.refresh(mBeanThemeHome.getNearbyCity());
            }
        }
        getContentView().setState(LoadingState.STATE_SUCCESS);
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void switchCity(CityEvent mCityEvent){
        if(mCityEvent!=null){
            if(mThemeExhibitonPresener!=null&&isCurrentVisibleState()){
                mThemeExhibitonPresener.getThemeExhibitonData( cityId=mCityEvent.getCityId());
            }else{
                tempCityID=mCityEvent.getCityId();
            }
        }


    }
}
